/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ruledapp.services.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import java.sql.Connection;
import java.sql.DriverManager;


/**
 *
 * @author klwul
 */
public class NewTestServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    Connection myConn=null;
    boolean isThere;

    @Override
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        String driverName = getInitParameter("jdbcDriver");
        String connURL = getInitParameter("dbServer");
         System.out.println(connURL);
            System.out.println(driverName);
        try{
           
            Class.forName(driverName).newInstance();
            myConn = DriverManager.getConnection(connURL,"root","claudiu89");
            System.out.println(myConn.toString());
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(myConn != null){
        try {
           String name = request.getParameter("name");
           String surname = request.getParameter("surname");
           String adresa = request.getParameter("adresa");
           String telefon = request.getParameter("telefon");
           String mail = request.getParameter("telefon");
           String cnp = request.getParameter("cnp");
           String q1 = request.getParameter("Q1");
           String q2 = request.getParameter("Q2");
           String q3a = request.getParameter("Q3a");
           String q3b = request.getParameter("Q3b");
           String q3c  = request.getParameter("Q3c");
           String q3d  = request.getParameter("Q3d");
           String q4i  = request.getParameter("nrEtaj");
           String q4ii = request.getParameter("totalEtaje");
           String q5a  = request.getParameter("Q5a");
           String q5b  = request.getParameter("Q5b");
           String q5c  = request.getParameter("Q5c");
           String q5d  = request.getParameter("Q5d");
           String q5e  = request.getParameter("Q5e");
           String q6 = request.getParameter("Q6");
           String q7 = request.getParameter("Q7");
           String q8 = request.getParameter("Q8");
           String q9 = request.getParameter("Q9");
           String q10a = request.getParameter("Q10a");
           String q10b = request.getParameter("Q10b");
           String q10c = request.getParameter("Q10c");
           String q10d = request.getParameter("Q10d");
           String q10e = request.getParameter("Q10e");
           String q10f = request.getParameter("Q10f");
           String q10g = request.getParameter("Q10g");
           String q10h = request.getParameter("Q10h");
           String q10i = request.getParameter("Q10i");
           String q10j = request.getParameter("Q10j");
           String q10k = request.getParameter("Q10k");
           String q11a = request.getParameter("Q11aNr");
           String q11b = request.getParameter("Q11bNr");
           String q11c = request.getParameter("Q11cNr");
           String q11d = request.getParameter("Q11dNr");
           String q11e = request.getParameter("Q11eNr");
           String q11f = request.getParameter("Q11fNr");
           String q11g = request.getParameter("Q11gNr");
           String q11h = request.getParameter("Q11hNr");
           String q11i = request.getParameter("Q11iNr");
           String q11j = request.getParameter("Q11jNr");
           String q11k = request.getParameter("Q11kNr");
           String q11l = request.getParameter("Q11lNr");
           String q11m = request.getParameter("Q11mNr");
           String q11n = request.getParameter("Q11nNr");
           String q11o = request.getParameter("Q11oNr");
           String q11p = request.getParameter("Q11pNr");
           String q11q = request.getParameter("Q11q");
           String q12a = request.getParameter("Q12a");
           String q12b1 = request.getParameter("Q12b1");
           String q12b2 = request.getParameter("Q12b2");
           String q12c = request.getParameter("Q12c");
           String q12d = request.getParameter("Q12d");
           String q12e = request.getParameter("Q12e");
           String q12f = request.getParameter("Q12f");
           String q13a = request.getParameter("Q13a");
           String q13b = request.getParameter("Q13b");
           String q13c = request.getParameter("Q13c");
           String q13d = request.getParameter("Q13d");
           String q13e = request.getParameter("Q13e");
           String q13f = request.getParameter("Q13f");
           String q14a = request.getParameter("Q14a");
           String q14b = request.getParameter("Q14b");
           String q14c = request.getParameter("Q14c");
           String q15a = request.getParameter("Q15a");
           String q15b = request.getParameter("Q15b");
           String q15c = request.getParameter("Q15c");
           String q15d = request.getParameter("Q15d");
           String q15e = request.getParameter("Q15e");
           String q16 = request.getParameter("Q16");
           String q17a = request.getParameter("Q17a");
           String q17b = request.getParameter("Q17b");
           String q18a = request.getParameter("Q18a");
           String q18b = request.getParameter("Q18b");
           String q18c = request.getParameter("Q18c");
           String q18d = request.getParameter("Q18d");
           String q18e = request.getParameter("Q18e");
           String q18f = request.getParameter("Q18f");
           String q18g = request.getParameter("Q18g");
           String q18h = request.getParameter("Q18h");
           String q18i = request.getParameter("Q18i");
           String q18j = request.getParameter("Q18j");
           String q18k = request.getParameter("Q18k");
           String q18l = request.getParameter("Q18l");
           String q18m = request.getParameter("Q18m");
           String q18n = request.getParameter("Q18n");
           String q18o = request.getParameter("Q18o");
           String q18p = request.getParameter("Q18p");
           String q18q = request.getParameter("Q18q");
           String q18r = request.getParameter("Q18r");
           String q18s = request.getParameter("Q18s");
           String q18t = request.getParameter("Q18t");
           String q19 = request.getParameter("Q19");

           String sql = "INSERT INTO dbo.tabel(Nume,Prenume,Adresa,Telefon,"+
                   "E_mail,cnp,1__a_sau_b,2_a_sau_b,3__a_,3__b_,3__c_,3__d_,4__etaj"+
                   ",tot_etaje,5__a_,5__b_,5__c_,5__d_,5__e_,6__anul,7__nr__loc_"
                   +",8__Aria,9__Inaltime,10__a_,10__b_,10__c_,10__d_,10__e_,"
                   +"10__f_,10__g_,10__h_,10__i_,10__j_,10__k_,11__a_,11__b_,"
                   +"11__c_,11__d_,11__e_,11__f_,11__g_,11__h_,11__i_,11__j_,"
                   +"11__k_,11__l_,11__m_,11__n_,11__o_,11__p_,11__q__tot,"
                   +"12__a_,12__b1_,12__b2_,12__c_,12__d_,12__e_,12__f_,13__a_,"
                   +"13__b_,13__c_,13__d_,13__e_,13__f_,14__a_,14__b_,14__c_,"
                   + "15__a_,15__b_,15__c_,15__d_,15__e_,16_a_sau_b,17__a1__a8,"
                   + "17__b1__b8,18__a_,18__b_,18__c_,18__d_,18__e_,18__f_,"
                   + "18__h___nu_exista_g_,18__i_,18__j_,18__k_,18__l_,18__m_,"
                   + "18__n_,18__o_,18__p_,18__q_,18__r__,18__s_,18__t_,"
                   + "18__u_,19__a__b)"
                   + "VALUES("+name+","+surname+","+adresa+","+telefon+","+mail+","+
                   cnp+","+q1+","+q2+","+q3a+","+q3b+","+q3c+","+q3d+","+q4i+","+q4ii+","+
                   q5a+","+q5b+","+q5c+","+q5d+","+q5e+","+q6+","+q7+","+q8+","+q9+","+
                   q10a+","+q10b+","+q10c+","+q10d+","+q10e+","+q10f+","+q10g+","+q10h+","+
                   q10i+","+q10j+","+q10k+","+q11a+","+q11b+","+q11c+","+q11d+","+
                   q11e+","+q11f+","+q11g+","+q11h+","+q11i+","+q11j+","+q11k+","+
                   q11l+","+q11m+","+q11n+","+q11o+","+q11p+","+q11q+","+
                   q12a+","+q12b1+","+q12b2+","+q12c+","+q12d+","+q12e+","+q12f+","+
                   q13a+","+q13b+","+q13c+","+q13d+","+q13e+","+q13f+","+q14a+","+
                   q14b+","+q14c+","+q15a+","+q15b+","+q15c+","+q15d+","+q15e+","+q16+","+q17a+","+
                   q17b+","+q18a+","+q18b+","+q18c+","+q18d+","+q18e+","+q18f+","+q18g+","+
                   q18h+","+q18i+","+q18j+","+q18k+","+q18l+","+q18m+","+q18n+","+q18o+","+
                   q18p+","+q18q+","+q18r+","+q18s+","+q18t+","+q19+")";
           System.out.println("sql este: " + sql);
           try{
           java.sql.Statement stmt = myConn.createStatement();
           System.out.println("Record added");
           int result = stmt.executeUpdate(sql);
           System.out.println("Record added2");
           if(result!=-1){
                        System.out.println("Record added");
                    }else{
                        System.out.println("The record couldn't be added");
                    }
            }catch(Exception e){
                e.printStackTrace();
            }

           System.out.println("numele este: " + name);
           System.out.println("Prenumele este: " + surname);
           System.out.println("Prima intrebare: " + q1);
           System.out.println("A doua intrebare: " + q2);
           System.out.println("Intrebarea 19: " + q19);
           


           // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TestServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet TestServlet at " + request.getContextPath () + "</h1>");
            out.println("<h2> Merge conexiunea </h2>");
            out.println("<h3> Nume introdus: " + name + "</h3>");
            out.println("<h4> Q19: " + q19 +"</h4>");
            out.println("</body>");
            out.println("</html>");

        }catch(Exception e){
            System.out.println(e.getMessage());
        } finally {
            out.close();
        }
        }else{
            System.out.println("NU merge");
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
